package com.tfy.liquor.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tfy.liquor.bean.MyPage;
import com.tfy.liquor.bean.VendorInfo;
import com.tfy.liquor.dao.VendorInfoDao;
import com.tfy.liquor.service.VendorInfoService;
import com.tfy.liquor.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class VendorInfoServiceImpl implements VendorInfoService {
    @Autowired
    private VendorInfoDao vendorInfoDao;

    // 添加供应商信息
    @Override
    public Boolean addVendorInfo(VendorInfo vendorInfo) {
        // 设置创建时间
        vendorInfo.setCreateTime(DateUtil.getNowDateTime());
        return vendorInfoDao.addVendorInfo(vendorInfo) > 0;
    }

    // 根据条件查询供应商信息
    @Override
    public MyPage getAllVendorInfo(Integer pageNum, Integer pageSize, VendorInfo vendorInfo) {
        // 如果传入的参数是负数，则默认为1
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<VendorInfo> list = vendorInfoDao.getAllVendorInfo(vendorInfo);
        return new MyPage((Page) list);
    }

    // 添加产品时获取正常的供应商信息
    @Override
    public ArrayList<VendorInfo> getVendorInfo() {
        return vendorInfoDao.getVendorInfo();
    }

    // 修改供应商信息
    @Override
    public Boolean editVendorInfo(VendorInfo vendorInfo) {
        return vendorInfoDao.editVendorInfo(vendorInfo) > 0;
    }

    // 修改供应商状态
    @Override
    public Boolean editVendorInfoStatus(VendorInfo vendorInfo) {
        return vendorInfoDao.editVendorInfoStatus(vendorInfo) > 0;
    }
}
